<?php

namespace App\Http\Controllers;
use App\Logic\MiGuLogic;
use Illuminate\Support\Facades\DB;

/**
 * RewardBox 领取累计登陆宝箱奖励
 */
class RewardBox extends Base
{

    public function logic()
    {

        $uid = $this->uid;
        if (!isset($this->param['box_id'])) {
            return $this->returnError(1, "param, box_id not set.\n");
        }
        $box_id = $this->param['box_id'];
        if ($box_id != 3 && $box_id != 7 && $box_id != 15 && $box_id != 21 && $box_id != 28) {
            return $this->returnError(2, "box_id error.\n");
        }

        $object = DB::select( "SELECT count(*) as count FROM t_reward_box_rec 
			where uid = $uid
			AND get_box_type = $box_id
			AND create_time >DATE_ADD(CURDATE(),INTERVAL -DAY(CURDATE())+1 DAY) 
			AND create_time <DATE_ADD(CURDATE()-DAY(CURDATE())+1,INTERVAL 1 MONTH )");

        if ($object[0]->count > 0) {
            return $this->returnError(3, "你已经领取了该宝箱.\n");
        }

        if (! DB::table('t_reward_box_rec')->insert(["uid" => $uid, "get_box_type" => $box_id])) {
            return $this->returnError(4, "领取宝箱插入记录错误.\n");
        }

        $add_gold     = 0;
        $add_diamonds = 0;
        if ($box_id == 3) {
            $add_gold     = 10000;
            $add_diamonds = 0;
        } else if ($box_id == 7) {
            $add_gold     = 30000;
            $add_diamonds = 0;
        } else if ($box_id == 15) {
            $add_gold     = 0;
            $add_diamonds = 50;
        } else if ($box_id == 21) {
            $add_gold     = 0;
            $add_diamonds = 70;
        } else if ($box_id == 28) {
            $add_gold     = 0;
            $add_diamonds = 100;
        }
        $ret = $this->grant_diamonds($uid, $add_diamonds, "签到宝箱", (string)$box_id,0,1,100004);
        if (!$ret) {
           return $this->returnError(5, "签到奖励钻石时，服务器发生数据错误.");
        }
        $ret = $this->grant_golds($uid, $add_gold, "签到宝箱", (string)$box_id);
		if (!$ret) {
            return $this->returnError(5, "签到奖励金币时，服务器发生数据错误.");
        }

		//领奖成功，查询玩家当前金币,钻石
        $object = DB::table('t_player_info')->where('id',$this->uid)->where('status',1)->first([
            'gold','migu_id'
        ]);

        $migu_info = MiGuLogic::instance()->get_migu_user_info((int)$object->migu_id);

		$ret_data                 = array();
		$ret_data["add_diamonds"] = $add_diamonds;
		$ret_data["add_gold"]     = $add_gold;
		$ret_data["gold"]         = $object->gold;
		$ret_data["diamonds"]     = $migu_info['diamond'];

		return $this->returnData($ret_data);

	}
}
